/**
 * The MIT License (MIT)
 *
 * Copyright (c) 2018-2019 Erik Moqvist
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/**
 * This file was generated by cantools version 39.4.5 Mon Apr 15 14:49:13 2024.
 */

#include <string.h>

#include "zt.h"

static inline uint8_t pack_left_shift_u8(
    uint8_t value,
    uint8_t shift,
    uint8_t mask)
{
    return (uint8_t)((uint8_t)(value << shift) & mask);
}

static inline uint8_t pack_left_shift_u16(
    uint16_t value,
    uint8_t shift,
    uint8_t mask)
{
    return (uint8_t)((uint8_t)(value << shift) & mask);
}

static inline uint8_t pack_right_shift_u16(
    uint16_t value,
    uint8_t shift,
    uint8_t mask)
{
    return (uint8_t)((uint8_t)(value >> shift) & mask);
}

static inline uint16_t unpack_left_shift_u16(
    uint8_t value,
    uint8_t shift,
    uint8_t mask)
{
    return (uint16_t)((uint16_t)(value & mask) << shift);
}

static inline uint8_t unpack_right_shift_u8(
    uint8_t value,
    uint8_t shift,
    uint8_t mask)
{
    return (uint8_t)((uint8_t)(value & mask) >> shift);
}

static inline uint16_t unpack_right_shift_u16(
    uint8_t value,
    uint8_t shift,
    uint8_t mask)
{
    return (uint16_t)((uint16_t)(value & mask) >> shift);
}

int zt_indicator_pack(
    uint8_t *dst_p,
    const struct zt_indicator_t *src_p,
    size_t size)
{
    if (size < 8u) {
        return (-EINVAL);
    }

    memset(&dst_p[0], 0, 8);

    dst_p[3] |= pack_left_shift_u8(src_p->dash_indicator, 2u, 0x0cu);

    return (8);
}

int zt_indicator_unpack(
    struct zt_indicator_t *dst_p,
    const uint8_t *src_p,
    size_t size)
{
    if (size < 8u) {
        return (-EINVAL);
    }

    dst_p->dash_indicator = unpack_right_shift_u8(src_p[3], 2u, 0x0cu);

    return (0);
}

int zt_indicator_init(struct zt_indicator_t *msg_p)
{
    if (msg_p == NULL) return -1;

    memset(msg_p, 0, sizeof(struct zt_indicator_t));

    return 0;
}

uint8_t zt_indicator_dash_indicator_encode(double value)
{
    return (uint8_t)(value);
}

double zt_indicator_dash_indicator_decode(uint8_t value)
{
    return ((double)value);
}

bool zt_indicator_dash_indicator_is_in_range(uint8_t value)
{
    return (value <= 3u);
}

int zt_ccvs_pack(
    uint8_t *dst_p,
    const struct zt_ccvs_t *src_p,
    size_t size)
{
    if (size < 8u) {
        return (-EINVAL);
    }

    memset(&dst_p[0], 0, 8);

    dst_p[1] |= pack_left_shift_u16(src_p->dash_vehicle_speed, 0u, 0xffu);
    dst_p[2] |= pack_right_shift_u16(src_p->dash_vehicle_speed, 8u, 0xffu);

    return (8);
}

int zt_ccvs_unpack(
    struct zt_ccvs_t *dst_p,
    const uint8_t *src_p,
    size_t size)
{
    if (size < 8u) {
        return (-EINVAL);
    }

    dst_p->dash_vehicle_speed = unpack_right_shift_u16(src_p[1], 0u, 0xffu);
    dst_p->dash_vehicle_speed |= unpack_left_shift_u16(src_p[2], 8u, 0xffu);

    return (0);
}

int zt_ccvs_init(struct zt_ccvs_t *msg_p)
{
    if (msg_p == NULL) return -1;

    memset(msg_p, 0, sizeof(struct zt_ccvs_t));

    return 0;
}

uint16_t zt_ccvs_dash_vehicle_speed_encode(double value)
{
    return (uint16_t)(value / 0.00390625);
}

double zt_ccvs_dash_vehicle_speed_decode(uint16_t value)
{
    return ((double)value * 0.00390625);
}

bool zt_ccvs_dash_vehicle_speed_is_in_range(uint16_t value)
{
    (void)value;

    return (true);
}

int zt_vehicle_status_pack(
    uint8_t *dst_p,
    const struct zt_vehicle_status_t *src_p,
    size_t size)
{
    if (size < 8u) {
        return (-EINVAL);
    }

    memset(&dst_p[0], 0, 8);

    dst_p[6] |= pack_left_shift_u8(src_p->current_gear, 0u, 0x0fu);

    return (8);
}

int zt_vehicle_status_unpack(
    struct zt_vehicle_status_t *dst_p,
    const uint8_t *src_p,
    size_t size)
{
    if (size < 8u) {
        return (-EINVAL);
    }

    dst_p->current_gear = unpack_right_shift_u8(src_p[6], 0u, 0x0fu);

    return (0);
}

int zt_vehicle_status_init(struct zt_vehicle_status_t *msg_p)
{
    if (msg_p == NULL) return -1;

    memset(msg_p, 0, sizeof(struct zt_vehicle_status_t));

    return 0;
}

uint8_t zt_vehicle_status_current_gear_encode(double value)
{
    return (uint8_t)(value);
}

double zt_vehicle_status_current_gear_decode(uint8_t value)
{
    return ((double)value);
}

bool zt_vehicle_status_current_gear_is_in_range(uint8_t value)
{
    return (value <= 15u);
}
